﻿using JShiwork.Devices.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace GYiwork.Devices.Printer
{
    public class clsMI9232 : Interface_Printer
    {
        #region 变量声明

        /// <summary>
        /// 喷码机实例化
        /// </summary>
        private MI_CIJ_9232 mMI9232 = new MI_CIJ_9232();
        //private SCC.CIJ.MI_CIJ_9232 mMI9232 = new SCC.CIJ.MI_CIJ_9232();

        /// <summary>
        /// 打印完成事件
        /// </summary>
        public event PrintMessageFinishedEventHandler PrintMessageFinished;
        /// <summary>
        /// 远程服务器事件
        /// </summary>
        public event HostErrEventHandler HostErrEvent;
        /// <summary>
        /// 错误描述
        /// </summary>
        public event ErrEventHandler ErrEvent;

        #endregion

        #region 构造函数

        public clsMI9232()
        {
            mMI9232.Err += new MI_CIJ_9232.ErrEventHandler(mMI9232_Err);
            mMI9232.HostErr += new MI_CIJ_9232.HostErrEventHandler(mMI9232_HostErr);
            mMI9232.PrintMessageFinished += new MI_CIJ_9232.PrintMessageFinishedEventHandler(mMI9232_PrintMessageFinished);
        }

        #endregion

        #region 方法定义

        /// <summary>
        /// 打开设备端口
        /// </summary>
        /// <param name="Port"></param>
        /// <param name="Baudrate"></param>
        /// <returns></returns>
        public bool _Open(int mPort, int mBaudrate)
        {
            //if (!mEcJetPrinter._Open(mPort, mBaudrate))
            //{
            //    MessageBox.Show("喷码机端口打开失败..." + mEcJetPrinter.ErrDes, "警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            //    return false;
            //}
            ////设备初始化
            //mEcJetPrinter.Fields = 1;
            return true;
        }
        /// <summary>
        /// 打开设备端口
        /// </summary>
        /// <param name="mAddr"></param>
        /// <param name="mPort"></param>
        /// <returns></returns>
        public bool _Open(string mAddr, int mPort)
        {
            if (!mMI9232._Open(mAddr, mPort))
            {
                MessageBox.Show("喷码机端口打开失败..." + mMI9232.ErrDes, "警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }
            
            mMI9232.UseExternalData = true;
            if (!mMI9232.Ini())
            {
                _Close();
                MessageBox.Show("喷码机初始化失败..." + mMI9232.ErrDes, "警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }
            mMI9232.Non_Doubled_Printed(false);
            _StartJob();
            return true;
        }
        /// <summary>
        /// 关闭设备端口
        /// </summary>
        public void _Close()
        {
            _StopJob();
            Thread.Sleep(200);
            mMI9232._Close();
        }
        /// <summary>
        /// 开启工作线程
        /// </summary>
        public void _StartJob()
        {
            mMI9232._StartJob();
        }
        /// <summary>
        /// 关闭工作线程
        /// </summary>
        public void _StopJob()
        {
            mMI9232._StopJob();
        }
        /// <summary>
        /// 发送打印数据
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public bool Print_A_Data(string[] mValue)
        {
            for (int i = 0; i < mValue.Length; i++)
            {
              //  mMI9232.FieldValue[i] = "QWERTYUIOPLKJHGFDSAZXCVBNMAAALKJHGFDSAQWRERTYUJIKOLMNHYGF,  ";
              //  mMI9232.FieldValue[i] = mValue[i].Substring(0, 35);
              //  mMI9232.FieldValue[i] = "ZHOU".PadRight(60, 'S');
                mMI9232.FieldValue[i] = mValue[i];
            }
            mMI9232.Fields = mValue.Length;
 
            bool ret = mMI9232.Print_A_Data();
            return ret;
        }

        #endregion 

        #region 响应事件
        /// <summary>
        /// 错误描述
        /// </summary>
        /// <param name="Description"></param>
        void mMI9232_Err(string Description)
        {
            if (ErrEvent != null)
                ErrEvent(Description);
        }
        /// <summary>
        /// 远程服务器
        /// </summary>
        /// <param name="Description"></param>
        void mMI9232_HostErr(string Description)
        {
            if (HostErrEvent != null)
                HostErrEvent(Description);
        }
        /// <summary>
        /// 打印完成事件
        /// </summary>
        /// <param name="PrintMessage"></param>
        void mMI9232_PrintMessageFinished(string PrintMessage)
        {
            if (PrintMessageFinished != null)
                PrintMessageFinished(PrintMessage);
        }
        #endregion

        #region Interface_Printer 成员


        #endregion
    }
}
